<?php
namespace frontend\controllers;

use common\models\CepingDetail;
use common\models\CepingReport;
use common\models\CepingResult;
use common\models\Chat;
use common\models\Img;
use common\models\Shopcart;
use Yii;
use common\models\UserMess;
use yii\data\Pagination;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
/**
 * Site controller
 */
class UsercenterController extends Controller
{

    public $layout="ucenter.php";
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],

            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],

        ];
    }
    //需要提前获取uid
<<<<<<< HEAD
    public function actionUserAccount($uid = 10207) {
        $this->layout='user-center.php';
        $userMess = UserMess::find()->where(['uid' => $uid])->one();
=======
    public function actionUserAccount() {

        $id = 6;
        $userMess = UserMess::find()->where(['id' => $id])->one();
    //    p($userMess);die;
>>>>>>> feature-dgq-libo
        return $this->render('user_account', ['model' => $userMess]);
    }
    public function actionTest_result(){

     //   p($uid);die;
        $testResult=CepingResult::find()->where(['userId'=>Yii::$app->user->id,'status'=>1])->all();
   //    p($testResult);die;
        return $this->render('test_result', ['testResult' => $testResult]);
    }
    public function actionIndex(){
        $uid=Yii::$app->user->id;
        $userMess=UserMess::find()->where(['id'=>$uid])->one();
        return $this->render('index', ['userMess' => $userMess]);

    }
    public function actionDelete($paper_id,$product_id){
        $uid=Yii::$app->user->id;
        $reports=CepingResult::find()->where(['paper_id'=>$paper_id,'userId'=>$uid,'product_id'=>$product_id])->all();

        $del=false;
        foreach($reports as $v){
            $v->status=2;
            if($v->save()) $del=true;
            else  $del=false;
        }
        if($del) echo "yes";
        else     echo "no";

}
    public function actionRename($paper_id,$name,$product_id){

        $uid=Yii::$app->user->id;
        $reports=CepingResult::find()->where(['paper_id'=>$paper_id,'userId'=>$uid,'product_id'=>$product_id])->all();

        $re=false;
        foreach($reports as $v){
            $v->name=$name;
            if($v->save()) $re=true;
            else  $re=false;
        }
        if($re)  echo "yes";
        else     echo "no";
    }
    public function actionChat(){

        $query = Chat::find()->where(['status' => 1,'userId'=>Yii::$app->user->id,'parent_id'=>0]);
        $countQuery = clone $query;
        $pages = new Pagination(['totalCount' => $countQuery->count()]);
        $pages->pageSize=5;
        $models = $query->offset($pages->offset)
            ->limit($pages->limit)
            ->all();

        return $this->render('chat', [
            'models' => $models,
            'pages' => $pages,
        ]);

<<<<<<< HEAD
    public function actionUserTesting($uid = 10207) {
        $this->layout='user-center.php';

        $testingResultArray[0] = new TestingResult("我的测评1", "2015.9.30");
        $testingResultArray[1] = new TestingResult("我的测评2", "2015.9.30");
        $testingResultArray[2] = new TestingResult("我的测评3", "2015.9.30");

        return $this->render('user_testing', ['testingResultArray' => $testingResultArray]);
    }

    public function actionUserFavorites($uid = 10207) {
        $this->layout='user-center.php';

        $testingResultArray[0] = new TestingResult("我的收藏1", "2015.9.30");
        $testingResultArray[1] = new TestingResult("我的收藏2", "2015.9.30");
        $testingResultArray[2] = new TestingResult("我的收藏3", "2015.9.30");

        return $this->render('user_favorites', ['testingResultArray' => $testingResultArray]);
    }

    public function actionChangeTestingName($newName, $uid) {
        Yii::trace("changing tesingname begin", "requests.testingname");
        Yii::trace($uid, "requests.testingname");
        Yii::trace($newName, "requests.testingname");
        //接收 请求参数
        /**********  更改数据库中评测的名字  *********/

        //传回 返回值
        echo "更改成功";
        // $this->response(array('status' => 0, 'data' => 'ok!'));

        Yii::trace("changing tesingname end", "requests.testingname");
    }

    public function actionDeleteTesting($uid, $testingId) {
        Yii::trace("delete testing begin", "requests.deleteTesting");
        Yii::trace($uid, "requests.deleteTesting");
        Yii::trace($testingId, "requests.deleteTesting");

        /*===============删除记录================*/

        Yii::trace("delete testing end", "requests.deleteTesting");    
    }

    public function actionViewTesting($testingId) {
        Yii::trace("view testing begin", "requests.deleteTesting");
        Yii::trace($testingId, "requests.deleteTesting");

        /*===============读取测试内容================*/

        /*显示*/
        $this->layout = "user-center.php";


        Yii::trace("view delete testing end", "requests.deleteTesting");
        return $this->render('view_testing', []);
    }

    public function actionSay($message = 'hello') {
        return $this->render('say', ['message' => $message]);
=======
>>>>>>> feature-dgq-libo
    }
    public function actionReply($content,$parent_id,$to){

        $chat=new Chat();
        $chat->content=$content;
        $chat->to=$to*1;
        $uid=Yii::$app->user->id;
        $chat->userId="$uid";
        $chat->username=Yii::$app->user->identity->username;
        $chat->parent_id=$parent_id*1;

        if($chat->save()){

            $img=Img::find()->where(['uid'=>$chat->userId])->one();
            if(isset($img)) {$chat->res1=Yii::$app->request->baseUrl.$img->img_small;}
            else $chat->res1=Yii::$app->request->baseUrl.'\img\demo.png';
            $chat->created_at=date('Y-m-d h:i',$chat->created_at);
            $chat->res2=Chat::findOne(['id'=>$chat->to])->username;

            return \Yii::createObject([
                'class' => 'yii\web\Response',
                'format' => \yii\web\Response::FORMAT_JSON,
                'data' => [
                    'obj' => $chat,
                    'code' => 100,
                ],
            ]);
        }
        else{
            echo "no";
            return false;
        }

    }



}

class TestingResult
{
    public $name;
    public $time;

    function __construct($name, $time) {
        $this->name = $name;
        $this->time = $time;
    }
}
